package net.lainiao.morethread.Chapter1.demo7;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/2/28.
 */
public class SearchTask implements Runnable {
    private Result result;

    public SearchTask(Result result) {
        this.result = result;
    }

    @Override
    public void run() {
        String name=Thread.currentThread().getName();
        System.out.println("Thread "+name+" Start");
        try {
            doTask();
            result.setName(name);
        }
        catch (InterruptedException e) {
            System.out.println("Thead "+Thread.currentThread().getName()+" interrupted");
            return ;
        }
        System.out.println("Thread "+name+" End");
    }

    private void doTask() throws InterruptedException {
        Random random=new Random();
        int value=(int)(random.nextDouble()*100);
        System.out.println("Thread Name "+Thread.currentThread().getName()+" Sleep "+value+" Seconds");
        TimeUnit.SECONDS.sleep(value);
    }
}
